/**
 * Project Name:cats-core-impl
 * File Name:StringUtil.java
 * Package Name:com.catsic.core.utils
 * Date:2014-3-6上午10:20:59
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */

package com.catsic.core.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.catsic.core.entity.Organ;

/**
 * ClassName: StringUtil Description: TODO date: 2014-3-6 上午10:20:59 author: Jay
 */

public class CoreUtils {

	/**
	 * toLowerCaseFirstOne:首字母转小写.
	 * 
	 * @author Jay
	 * @param s
	 * @return
	 */

	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder())
					.append(Character.toLowerCase(s.charAt(0)))
					.append(s.substring(1)).toString();
	}

	/**
	 * toUpperCaseFirstOne:首字母转大写.
	 * 
	 * @author Jay
	 * @param s
	 * @return
	 */

	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder())
					.append(Character.toUpperCase(s.charAt(0)))
					.append(s.substring(1)).toString();
	}
	
	/**
	 * configerations:加载server.properties资源文件
	 * @author yangyd
	 * @return
	 * @throws IOException
	 */
	public static Properties configerations() throws IOException{
		
		Resource resource = new ClassPathResource("/server.properties");
		Properties props = PropertiesLoaderUtils.loadProperties(resource);
		return props;
	}

	/**
	 * 单一排序，升序
	 * @param t
	 * @param order
	 * @return
	 */
	public static <T> List<T> asc(List<T> t,String order){
		Collections.sort(t, new BeanComparator<T>(order));
		return t;
	}
	
	/**
	 * @param t
	 * @param map key为排序字段名称，value为排序方式，0降序，1升序
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> sort(List<T> list,Map<String,String> map){
		Comparator<T> comparator = ComparableComparator.getInstance();
		comparator = ComparatorUtils.reversedComparator(comparator); //逆序 
        List<Object> sortFields = new ArrayList<Object>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
        	if(StringUtils.equals("0", entry.getValue())){
        		sortFields.add(new BeanComparator<T>(entry.getKey(), comparator));
        	}else if(StringUtils.equals("1", entry.getValue())){
        		sortFields.add(new BeanComparator<T>(entry.getKey()));
        	}
    	}
        ComparatorChain multiSort = new ComparatorChain(sortFields);
        Collections.sort(list,multiSort);
		return list;
	}
	/*
	public static String getCode(String ptcode, String cdcode, int digit){
		int length = ptcode.length();
		int count = length/digit;
		while(count != 0){
			String temp = ptcode.substring((count-1)*digit, count*digit);
			if(!StringUtils.equals(temp, "0000")){
				break;
			}
			count--;
		}
		return digit;
	}*/
	
	/**
	 * 获取当前登录人的二级机构
	 * getSecondOrgan:(这里用一句话描述这个方法的作用).
	 * @author yangyudi
	 * @param organ
	 */
	public static Organ getSecondOrgan(Organ organ){
		Organ torgan = null;
		while(organ.getParent() != null && 2 != organ.getJb()){
			torgan = organ.getParent();
		}
		return torgan;
	}
	/**
	 * substring:截取字符串，转化为list
	 * @author yangyudi
	 * @param string
	 * @return
	 */
	public static List<String> substring(String string,int digit) {  
        StringBuffer sb = new StringBuffer(string);  
        List<String> list = new ArrayList<String>();  
        int start = 0;  
        int end = start + digit;  
          
        while(start<sb.length()){  
            if(start>=sb.length())  
                return list;  
            String temp = sb.substring(start, end);  
            list.add(temp);  
            start = end;  
            end = end + digit;  
            if(end>=sb.length()){  
                end = sb.length();  
            }  
        }
        return list;
    }   
}
